<?php	if ( ! defined('BASEPATH')) exit('No direct script access allowed');

function thumbs()
{
	$CI =& get_instance();
	return $CI->config->item('thumbs');
}

function thumbs_lib()
{
	$CI =& get_instance();
	return $CI->config->slash_item('thumbs_lib');
}

function read_img_thumb($thumb)
{
	$CI =& get_instance();
	$arr_thumbs=$CI->config->item('thumbs');
	$name_dir=(in_array($thumb,$arr_thumbs)) ? 'thumb-'.$thumb : ''; 
	return $name_dir;
}

function uploads_path()
{
	$CI =& get_instance();
	return $CI->config->slash_item('uploads_path');
}

function img_url($thumb=NULL,$image_name)
{
	$CI=& get_instance();
	$uploads_url=$CI->config->slash_item('uploads_url');
	$dir_thumb='';
	if($thumb){
		$dir_thumb=read_img_thumb($thumb);
	}
	return site_url($uploads_url.$dir_thumb).'/'.$image_name;
}

function re_img_url($thumb=NULL,$img_url=NULL){
	//preg_match('/thumb-(.*?)\//i',$img_url,$q);
	return preg_replace('/([0-9]{0,4}\_[0-9]{0,4})\//i',$thumb.'/',$img_url);
}

function crop_img(){
	include_once (thumbs_lib()."ThumbLib.inc.php");
	$thumb = PhpThumbFactory::create('http://phpthumb.gxdlabs.com/wp-content/themes/phpthumb/images/header_bg.png');
	$thumb->resize(100, 100);
	$thumb->show();
}

/*
 * modify by: manhnv
 * date: 19/10/2010
 */
function show_img_url($img_url=NULL,$thumb='300_200',$w=300){
	$url = site_url($img_url);
	if($img = @GetImageSize($url)){
		preg_match('/uploads\//',$img_url,$u);
		if($u){
			return preg_replace('/([0-9]{0,4}\_[0-9]{0,4})\//i',$thumb.'/',$url);
		}else{
			return $url;
		}
	}
	else{
		switch($w){
			case 300 :
				$img_name = '300x200.gif';
				break;
			case 210 :
				$img_name = '210x150.gif';
				break;
			case 100 :
				$img_name = '100x70.gif';
				break;
			case 65 :
				$img_name = '65x49.gif';
				break;
		}
		return client_img($img_name);
	}
}
/*
 * modify by: manhnv
 * date: 19/10/2010
 */

/*
 * client
 * author: manhnv
 * date  :24-09-10
 */

function client_css($filename=NULL){
	$CI =& get_instance();
	$client_path = $CI->config->slash_item('client_path');
	$client_css = $CI->config->slash_item('client_css');
	return site_url($client_path.$client_css).'/'.$filename;
}

function client_js($filename=NULL){
	$CI =& get_instance();
	$client_path = $CI->config->slash_item('client_path');
	$client_js = $CI->config->slash_item('client_js');
	return site_url($client_path.$client_js).'/'.$filename;
}

function client_img($filename=NULL){
	$CI =& get_instance();
	$client_path = $CI->config->slash_item('client_path');
	$client_images = $CI->config->slash_item('client_images');
	return site_url($client_path.$client_images).'/'.$filename;
}

function client_flash($filename=NULL){
	$CI =& get_instance();
	$client_path = $CI->config->slash_item('client_path');
	$client_flash = $CI->config->slash_item('client_flash');
	return site_url($client_path.$client_flash).'/'.$filename;
}

function get_title($match = '',$replace = ''){
	$CI =& get_instance();
	return str_replace($match,$replace,$CI->config->item('title'));
}

function get_description($match = '',$replace = ''){
	$CI =& get_instance();
	return str_replace($match,$replace,$CI->config->item('description'));
}

function get_keywords($match = '',$replace = ''){
	$CI =& get_instance();
	return str_replace($match,$replace,$CI->config->item('keywords'));
}

/*
 * get number records in config
 * date: 11/11/2010
 * param: name in config == $config['name...'] = '?';
 * return: int
 */

function get_conf_num_rows($name_config = NULL){
	$CI =& get_instance();
	$num = (intval($CI->config->item($name_config)) > 0) ? intval($CI->config->item($name_config)) : 1;
	return $num;
}

function get_item_conf($item){
	$CI =& get_instance();
	return $CI->config->item($item);
}

function get_slash_item_conf($slash_item){
	$CI =& get_instance();
	return $CI->config->slash_item($slash_item);
}

/*
 * end get_num_rows
 */

/* -----------------------------------------------------------------------------
 * admin helper
 * @author: hnx116
 * @date: 01-10-2010
 * ---------------------------------------------------------------------------*/

function admin_folder(){
	$CI =& get_instance();
	$name = ($CI->config->item('admin_folder')) ? $CI->config->item('admin_folder') : 'admin';
	return $name;
}
 
function admin_css($filename = NULL) {
	$CI =& get_instance();
	$admin_path = $CI->config->slash_item('admin_path');
	$admin_css 	= $CI->config->slash_item('admin_css');
	
	return site_url($admin_path.$admin_css).'/'.$filename;
}

function admin_js($filename = NULL) {
	$CI =& get_instance();
	$admin_path = $CI->config->slash_item('admin_path');
	$admin_js 	= $CI->config->slash_item('admin_js');
	return site_url($admin_path.$admin_js).'/'.$filename;
}

function admin_img($filename = NULL) {
	$CI =& get_instance();
	$admin_path = $CI->config->slash_item('admin_path');
	$admin_images = $CI->config->slash_item('admin_images');
	return site_url($admin_path.$admin_images).'/'.$filename;
}

function admin_flash($filename = NULL){
	$CI =& get_instance();
	$admin_path = $CI->config->slash_item('admin_path');
	$admin_flash = $CI->config->slash_item('admin_flash');
	return site_url($admin_path.$admin_flash).'/'.$filename;
}
